import Core.Basic
from Core.package import *




driver : WebDriver = object()
wait : WebDriver = object()
actions : ActionChains = object()
pddconfigfile : str = object()



class WorkerThread(QThread):
    def run(self):
        global driver,wait,actions,pddconfigfile
        driver.switch_to.new_window("tab")
        configdata = json.loads(s=open(file=pddconfigfile,mode="r",encoding='utf-8').read())
        
        
        # download main image
        mainloacl_path_list = []
        for index,url in enumerate(configdata["main_images_src_url_list"],start=1):
            download_image_with_selenium(driver=driver,url=url,save_filename=f"{os.getcwd()}/downloads/Main-{index:02d}.jpg")
            mainloacl_path_list.append(f"{os.getcwd()}/downloads/Main-{index:02d}.jpg")
            
        # download detail image
        detaiillocal_path_list = []
        for index,url in enumerate(configdata["detail_images_src_url_list"][:-1],start=1):
            download_image_with_selenium(driver=driver,url=url,save_filename=f"{os.getcwd()}/downloads/Detail-{index:02d}.jpg")
            detaiillocal_path_list.append(f"{os.getcwd()}/downloads/Detail-{index:02d}.jpg")
        
        # download sku image
        skulocal_path_list = []
        for index,url in enumerate(configdata["colors_list"],start=1):
            download_image_with_selenium(driver=driver,url=url['image_src_url'],save_filename=f"{os.getcwd()}/downloads/Sku-{index:02d}.jpg")
            skulocal_path_list.append(f"{os.getcwd()}/downloads/Sku-{index:02d}.jpg")
    
        
        # goto pdd create page
        Goto_Select_Category(driver=driver,wait=wait,actions=actions)
        
        Select_First_Level_Category(driver=driver,wait=wait,actions=actions)
        
        Select_Secondary_Category(driver=driver,wait=wait,actions=actions)
        
        Goto_Parameter_Input(driver=driver,wait=wait,actions=actions)
    
        
        InputProductName(driver=driver,wait=wait,actions=actions,product_name=configdata["title"])
        
        for _ in mainloacl_path_list:
            UploadMainPicture(driver=driver,wait=wait,actions=actions,picture_name=f"{_}")
    
        for _ in detaiillocal_path_list:
            UploadDetailPicture(driver=driver,wait=wait,actions=actions,picture_name=f"{_}")
        
        
        

        
        # print(configdata["detail_images_src_url_list"][0])
        # download_image_with_selenium(driver=driver,url=configdata["detail_images_src_url_list"][0],save_filename=f"{os.getcwd()}/Main-01.jpg")
        # download_image_with_selenium(driver=driver,url=configdata["detail_images_src_url_list"][1],save_filename=f"{os.getcwd()}/Main-02.jpg")
        # download_image_with_selenium(driver=driver,url=configdata["detail_images_src_url_list"][2],save_filename=f"{os.getcwd()}/Main-03.jpg")
        # download_file(driver=driver,url=configdata["detail_images_src_url_list"][0],save_filename=f"{os.getcwd()}/Main-01.jpg")
        # download_file(driver=driver,url=configdata["detail_images_src_url_list"][1],save_filename=f"{os.getcwd()}/Main-02.jpg")
        # download_file(driver=driver,url=configdata["detail_images_src_url_list"][2],save_filename=f"{os.getcwd()}/Main-03.jpg")
        
        # Goto_Select_Category(driver=driver,wait=wait,actions=actions)
        # Select_First_Level_Category(driver=driver,wait=wait,actions=actions)
        # Select_Secondary_Category(driver=driver,wait=wait,actions=actions)
        # Goto_Parameter_Input(driver=driver,wait=wait,actions=actions)
        
        
        
    


class Qt_Custom_Staff_Class(QObject):
    Start_Task = Signal()
    Task_Finsh = Signal()
    Current_Completion_Process = Signal(int)
    Emergency_Stop = Signal()
    OnlyRun = Signal()
        
    def __init__(self, parent=None):
        super().__init__(parent)
        self.Start_Task.connect(self.RunTask)
        
    @Slot(str)
    def solt_fun(self,text):
        print(f"我是槽函数,我是 {text}")

    # 设置员工属性
    def Set_Staff_Attribute(self,**kwargs):
        pass

    def RunTask(self,parm_driver,parm_wait,parm_actions,configfile):
        global driver,wait,actions,pddconfigfile
        
        driver = parm_driver
        wait = parm_wait
        actions = parm_actions
        pddconfigfile = configfile
        
        self.workerthread = WorkerThread()
        self.workerthread.finished.connect(lambda : self.Task_Finsh.emit())
        self.workerthread.start()